package com.xnsy.jishexiangmu.controller;

import com.xnsy.jishexiangmu.pojo.*;
import com.xnsy.jishexiangmu.service.user.UserService;
import com.xnsy.jishexiangmu.uitl.JwtUtil;
import com.xnsy.jishexiangmu.uitl.Md5Util;
import com.xnsy.jishexiangmu.uitl.Result;
import com.xnsy.jishexiangmu.uitl.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        User u = userService.findByUsername(user.getUsername());
        if (u != null) {
            return Result.error("该用户已存在");
        } else {
            userService.register(user.getUsername(), user.getPassword());
            return Result.success("注册成功");
        }
    }
    @PostMapping("/login")
    public Result login(@RequestBody Map<String, String> user){
        String username = user.get("username");
        User getuser = userService.findByUsername(username);
        if (getuser == null){
            return Result.error("该用户不存在，请先注册");
        }else {
            String password = user.get("password");
            if( !Md5Util.getMD5String(password).equals(getuser.getPassword())){
                return Result.error("密码不正确,请重新输入");
            }
//            生成Jwt令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put("username", getuser.getUsername());
//            待放入权限和认证信息


            String token = JwtUtil.genToken(claims);
            return Result.success(token);
        }
    }

    //后端用登录接口
    @PostMapping("/login2")
    public Result login2(@RequestBody Map<String, String> user){
        String username = user.get("username");
        User getuser = userService.findByUsername(username);
        if (getuser == null){
            return Result.error("该用户不存在，请先注册");
        }else {
            String password = user.get("password");
            if( !Md5Util.getMD5String(password).equals(getuser.getPassword())){
                return Result.error("密码不正确,请重新输入");
            }
//            生成Jwt令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put("username", getuser.getUsername());
            claims.put("toWhere", getuser.getToWhere());
//            待放入权限和认证信息
            WebUser webUser = new WebUser();
            String token = JwtUtil.genToken(claims);
            webUser.setToken(token);
            webUser.setAuthentication(getuser.getAuthentication());
            webUser.setUsername(getuser.getUsername());
            webUser.setIsroot(getuser.getIsroot());
            webUser.setToWhere(getuser.getToWhere());
            return Result.success(webUser);
        }
    }

    @PutMapping("/updatepassword")
    public Result updatePassword (@RequestBody Map<String, String> map){
//        通过jwt令牌获取用户名
        Map<String, Object> map1 = ThreadLocalUtil.get();
        String username = (String) map1.get("username");
//         获取用户账号信息
        User user = userService.findByUsername(username);
        String oldpassword = map.get("oldpassword");
        if (!Md5Util.getMD5String(oldpassword).equals(user.getPassword())) {
            return Result.error("原密码不正确");
        }
        String newpassword = map.get("newpassword");
        String repassword = map.get("repassword");
        if(!newpassword.equals(repassword)){
            return Result.error("2次输入密码不相同");
        }
        userService.updatePassword(newpassword, username);
        return Result.success("修改成功");
    }
    @GetMapping("/getusermessage")
    public Result<UserMessage> getUserMessage(){
        UserMessage userMessage = userService.getUserMessage();
        return Result.success(userMessage);
    }
    @PutMapping("/updateusermessage")
    public Result updateUserMessage(@RequestBody UserMessage userMessage){
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        userMessage.setUsername(username);
        userService.updateUserMessage(userMessage);
        return Result.success("修改成功");
    }
    // 认证申请
    @GetMapping("/renzheng")
    public Result rezheng(@RequestParam String authentication,@RequestParam String toWhere){
        userService.renzheng(authentication, toWhere);
        return Result.success("申请成功,请等待管理员同意");
    }
    // 获取用户认证处理列表
    @GetMapping("getrenzheng")
    public Result getrenzheng(){
        List<RenZheng> renZhengs = userService.getrenzheng();
        return Result.success(renZhengs);
    }
    // 处理用户认证请求
    @PostMapping("/setrenzheng")
    public Result setRengZheng(@RequestBody Map<String, String> lists){
        String username = lists.get("username");
        String authentication = lists.get("authentication");
        String order = lists.get("order");
        String toWhere = lists.get("toWhere");
        Integer isaccept = Integer.valueOf(order);
        userService.setRengZheng(username, isaccept, authentication,toWhere);
        return Result.success("处理成功");
    }

    // 获取当前认证下的所有用户
    @PostMapping("/getalluser")
    public Result getAllUser(@RequestBody Map<String, String> user){
        String isroot = user.get("isroot");
        List<User> users;
        if (isroot.equals("1")){
            String s = user.get("authentication");
           users = userService.getAllUser(s);
        }else{
            return Result.success("当前用户非管理员");
        }
        return Result.success(users);
    }

    @GetMapping("/setroot")
    public Result setRoot(@RequestParam Integer isroot, @RequestParam Integer uid){
        userService.setRoot(isroot, uid);
        return Result.success("设置成功");
    }
    // 提交举报信息
    @PostMapping("/jubao")
    public Result postMessage(@RequestBody JuBao juBao){
        userService.postMessage(juBao);
        return Result.success("提交成功");
    }
    @GetMapping("/getjindu")
    // 获取举报进度
    public Result getJinDu (){
        int i = userService.getJinDu();
        return Result.success(i);
    }

    // 获取举报信息
    @GetMapping("/getjubao")
    public Result getJuBao(){
        List<JuBao> juBao = userService.getJuBao();
        return Result.success(juBao);
    }
    // 处理举报信息
    @GetMapping("/setjindu")
    public Result setJinDu(@RequestParam Integer id){
        userService.setJinDu(id);
        return Result.success("处理成功");
    }
}
